<?php 
	if(!isset($_SESSION)){
		session_start();
	}

	// Clear previous messages.
	if(isset($_SESSION['error'])) session_unregister('error');
	if(isset($_SESSION['success'])) session_unregister('success');
	if(isset($_SESSION['sales'])) session_unregister('sales');
	
	// Get values from the page.
	$orderDate = $_POST['orderDate'];
	
	// Check for mandatory fields.
	if(empty($orderDate)){
		$_SESSION['error'] = "Pleace Select order Date";
	}else{
		include("../dbconnection.php");
		if($result = mysql_query("SELECT itemName, order_items.itemCode, SUM(quantity), SUM(price), units.unitCode FROM order_items, orders, items, units WHERE items.itemCode=order_items.itemCode AND orders.orderId = order_items.orderId AND orders.orderdate='$orderDate' AND units.unitId=items.itemUnit GROUP BY order_items.itemCode")){
			if(mysql_num_rows($result) > 0){
				$output[] = "<table width='100%' border='1px' cellspacing='0px' class='masterTable' >";
				$output[] = "<tr><h3>Daily Revenue</h3></tr>";
				$output[] = "<tr><th>Item Name</th>";
				$output[] = "<th>Item Code</th>";
				$output[] = "<th>Quantity</th>";
				$output[] = "<th>Price (Rs/=)</th></tr>";
				while($row=mysql_fetch_array($result)){
					$output[] = "<tr><td>".$row['itemName']."</td>";
					$output[] = "<td>".$row['itemCode']."</td>";
					$output[] = "<td>".$row['SUM(quantity)']."  " .$row['unitCode']."</td>";
					$output[] = "<td>".$row['SUM(price)']."</td></tr>";
				}
				//if($result2 = mysql_query("SELECT SUM(price) FROM order_items, orders WHERE orders.orderId = order_items.orderId AND orders.orderdate='$orderDate' GROUP BY order_items.orderId")){
					//if($row2=mysql_fetch_array($result2)){}
					//else{	$_SESSION['error'] = $orderDate." is not available";
					//}
				//}
				//$output[] = "<tr style='background:#abc'><td colspan='3px'><b>Total</b></td><td>".$row2['SUM(price)']."</td></tr>";
				$output[] = "</table>";
				$_SESSION['sales'] = join('',$output);
				echo $_SESSION['sales'] ;
			}else{
				$_SESSION['error'] = $orderDate." is not available";
			}
		}else{
			$_SESSION['error'] = "Error Occured.".mysql_error();		
		}
	}
	$_SESSION['page'] = "viewSales.php";
	header('Location: ../../');	
?>